// 内置姓名列表
//const names = [];

//从环境变量中读取姓名列表
const names = process.env.NAMES 

let remaining = [];
let called = [];
let timer;

let audioManager = new AudioManager(window.AUDIO_CONFIG);

function startRoll() {
    if (remaining.length === 0) return;
    
    // 启动随机滚动
    document.getElementById('startBtn').disabled = true;
    document.getElementById('stopBtn').disabled = false;
    
    timer = setInterval(() => {
	const index = Math.floor(Math.random() * remaining.length);
	document.getElementById('display').textContent = remaining[index];
    }, 50);

    // 1秒后自动停止
    setTimeout(() => {
        const current = stopRoll();
	audioManager.play(current);
    }, 1000);
}

function stopRoll() {
    clearInterval(timer);
    document.getElementById('startBtn').disabled = false;
    document.getElementById('stopBtn').disabled = true;

    const current = document.getElementById('display').textContent;
    const index = remaining.indexOf(current);
    
    if (index > -1) {
	remaining.splice(index, 1);
	called.push(current);
	updateLists();
    }

    return current
}

function resetList() {
    remaining = [...names];
    called = [];
    updateLists();
    document.getElementById('display').textContent = "已重置";
}

function updateLists() {
    document.getElementById('remainingList').innerHTML = 
	remaining.map(name => `<span>${name}</span>`).join('');
    
    document.getElementById('calledList').innerHTML = 
	called.map(name => `<span>${name}</span>`).join('');
    
    document.getElementById('remainingCount').textContent = remaining.length;
    document.getElementById('calledCount').textContent = called.length;
}

// 初始化
resetList();
